package day3;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.connectors.redis.RedisSink;
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class job3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        env.enableCheckpointing(5000);

        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("bigdata1:9092")
                .setGroupId("day job2")
                .setTopics("order")
                .setStartingOffsets(OffsetsInitializer.earliest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        FlinkJedisPoolConfig config = new FlinkJedisPoolConfig.Builder()
                .setHost("bigdata1")
                .setPort(6379)
                .setPassword("123456")
                .build();


        WatermarkStrategy<String> watermarks = WatermarkStrategy.<String>noWatermarks();
        OutputTag<String[]> string = new OutputTag<>("string", TypeInformation.of(String[].class));
        SingleOutputStreamOperator<String> process = env.fromSource(source, watermarks, "day3 job2")
                .map(s -> s.split(","))
                .process(new ProcessFunction<String[], String>() {
                    ValueState<Integer> stateAmount;
                    ValueState<Integer> allAmonut;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        stateAmount = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("state_amount", Integer.TYPE));
                        allAmonut = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("all_amonut", Integer.TYPE));
                    }

                    @Override
                    public void processElement(String[] s, ProcessFunction<String[], String>.Context context, Collector<String> collector) throws Exception {
                        Integer value = allAmonut.value();
                        Integer value1 = stateAmount.value();
                        value++;
                        if (s[5].equals("1003")) {
                            value1++;
                            context.output(string, s);
                        }
                        allAmonut.update(value);
                        stateAmount.update(value1);
                        collector.collect(String.valueOf(stateAmount.value() / allAmonut.value()));
                    }
                });

        process.addSink(new RedisSink<>(config, new RedisSink2()));
        SideOutputDataStream<String[]> sideOutput = process.getSideOutput(string);
        sideOutput.addSink(new HbaseSink());

        env.execute();
    }
}
